/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.util.i18n;

import java.util.Arrays;
import java.util.Locale;

import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

/**
 * NemosTranslator : The Nemos simple translator.
 * 
 * <p>
 * This translator provides a simple way to translate and format messages
 * without handling the availability of a {@link ITranslator} service.
 * </p>
 * 
 * @author Romain DUBOIS
 */
public class NemosTranslator implements ITranslator {

    /** The log service tracker */
    private final ServiceTracker<ITranslator, ITranslator> mTranslatorTracker;

    /**
     * Constructor
     * 
     * @param pContext
     *            The calling bundle context
     */
    public NemosTranslator(final BundleContext pContext) {
        super();

        mTranslatorTracker = new ServiceTracker<ITranslator, ITranslator>(pContext,
                ITranslator.class, null);
        mTranslatorTracker.open();
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.util.i18n.ITranslator#getLabel(java.lang.String,
     *      java.lang.Object[])
     */
    @Override
    public String getLabel(final String pKey, final Object... pParams) {
        final ITranslator lTranslator = mTranslatorTracker.getService();
        if (lTranslator == null) {
            return getDefaultText(pKey, pParams);
        }
        return lTranslator.getLabel(pKey, pParams);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.util.i18n.ITranslator#translateLabel(java.lang.String,
     *      java.util.Locale, java.lang.Object[])
     */
    @Override
    public String translateLabel(final String pKey, final Locale pLocale, final Object... pParams) {
        final ITranslator lTranslator = mTranslatorTracker.getService();
        if (lTranslator == null) {
            return getDefaultText(pKey, pParams);
        }
        return lTranslator.translateLabel(pKey, pLocale, pParams);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.util.i18n.ITranslator#getText(java.lang.String,
     *      java.lang.String, java.lang.Object[])
     */
    @Override
    public String getText(final String pBaseName, final String pKey, final Object... pParams) {
        final ITranslator lTranslator = mTranslatorTracker.getService();
        if (lTranslator == null) {
            return getDefaultText(pKey, pParams);
        }
        return lTranslator.getText(pBaseName, pKey, pParams);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.util.i18n.ITranslator#translateText(java.lang.String,
     *      java.lang.String, java.util.Locale, java.lang.Object[])
     */
    @Override
    public String translateText(final String pBaseName, final String pKey, final Locale pLocale,
            final Object... pParams) {
        final ITranslator lTranslator = mTranslatorTracker.getService();
        if (lTranslator == null) {
            return getDefaultText(pKey, pParams);
        }
        return lTranslator.translateText(pBaseName, pKey, pLocale, pParams);
    }

    /**
     * The default format method used when no translator is available.
     * 
     * @param pKey
     *            The label key
     * @param pParams
     *            The label parameters
     * @return The default label
     */
    private String getDefaultText(final String pKey, final Object[] pParams) {
        // No parameters : raw key
        if (pParams == null || pParams.length == 0) {
            return pKey;
        }

        // Append parameters
        return pKey + Arrays.toString(pParams);
    }

    /**
     * Close the translator.
     */
    public void close() {
        mTranslatorTracker.close();
    }
}
